package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnAnimal;
import com.animal.entity.AnInvitation;
import com.animal.entity.AnOrder;
import com.animal.entity.AnUser;
import com.animal.mapper.AnAnimalMapper;
import com.animal.response.Result;
import com.animal.service.AnAnimalService;
import com.animal.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-28
 */
@RestController
@RequestMapping("/an-animal")
public class AnAnimalController extends JSONAuthentication {

    @Autowired
     private AnAnimalMapper anAnimalMapper;
    @Autowired
    private AnAnimalService anAnimalService;

    @ApiOperation(value = "添加动物")
    @PostMapping("/add")
    public void addAnimal(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("anName") String anname,@RequestParam("type") int type,@RequestParam("rescueTypeId") int  rescueTypeId ,@RequestParam("donationPrice") float donationPrice
    ,@RequestParam("rescueCause") String rescueCause,@RequestParam("anStatus") int anStatus,@RequestParam("anPerson") int anPerson,@RequestParam("price") float price,@RequestParam("anCondition") String anCondition,@RequestParam("adopt") int adopt,@RequestParam("adoptId") int adoptId,@RequestParam("donationId") int donationId
    ) throws  Exception{
        AnAnimal animal=new AnAnimal();
        animal.setAnName(anname);
        animal.setPrice(price);
        animal.setType(type);
        animal.setRescueTypeId(rescueTypeId);
        animal.setDonationPrice(donationPrice);
        animal.setRescueCause(rescueCause);
        animal.setAnStatus(anStatus);
        animal.setAnPerson(anPerson);
        animal.setPrice(price);
        animal.setAnCondition(anCondition);
        animal.setAdopt(adopt);
        animal.setAdoptId(adoptId);
        animal.setDonationId(donationId);

        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        animal.setPicture(avatar_url);
        response.reset();

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        animal.setAnCreatTime(timestamp);

      if(anAnimalMapper.selectByName(anname)!=null){
          Result error = Result.error().message("该动物名字已存在");
          this.WriteJSON(request, response, error);
      }else{
          int i=anAnimalMapper.insert(animal);
          if(i>0){
              AnAnimal animal1=anAnimalMapper.selectByName(anname);

              Result ok = Result.ok();
              ok.data("an_id",animal1.getAnId());
              this.WriteJSON(request, response, ok);
          } else {
              Result error = Result.error();
              this.WriteJSON(request, response, error);
          }
      }
    }
    @ApiOperation(value = "删除动物")
    @PostMapping("/del")
    public void delAnimal(HttpServletRequest request, HttpServletResponse response, @RequestParam("anId") int anId ) throws Exception{
        int i=anAnimalMapper.deleteById(anId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改动物")
    @PostMapping("/upd")
    public void updAnimal(HttpServletRequest request, HttpServletResponse response, @RequestBody AnAnimal animal)throws Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        animal.setAnUpdTime(timestamp);

        int i=anAnimalMapper.updateById(animal);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询动物")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total=anAnimalMapper.selectCount(null);
        List<AnAnimal> list =anAnimalService.findAll(current,size);
        Result ok = Result.ok().data("anamial",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "根据动物名字查询动物")
    @PostMapping("/findByname")
    public void findByname(HttpServletRequest request, HttpServletResponse response, @RequestParam("anName") String anNmae)throws  Exception{
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("an_name",anNmae);
        AnAnimal list =anAnimalMapper.selectByName(anNmae);
        if(list!=null){
            Result ok = Result.ok().data("anamial",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有该名字的动物");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "根据领养人id项目查询动物")
    @PostMapping("/lookByUserId")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("userId") int  userId)throws  Exception{
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("adopt_id",userId);
        int total=anAnimalMapper.selectCount(wrapper);
        List<AnAnimal> list =anAnimalService.findByUserId(size,current,userId);

            Result ok = Result.ok().data("anamial",list);
            ok.data("total",total);
            this.WriteJSON(request, response, ok);

    }
    @ApiOperation(value = "按动物id查询动物")
    @PostMapping("/findById")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("anId") int  anId)throws  Exception{
        AnAnimal list=  anAnimalMapper.selectById(anId);

        if(list!=null){
            Result ok = Result.ok().data("product",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此动物");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "修改动物状态通过动物id")
    @PostMapping("/updAnByid")
    public void updOrderByid(HttpServletRequest request, HttpServletResponse response,@RequestParam("anId") int anId,@RequestParam("adopt") int adopt) throws  Exception{
        AnAnimal animal=anAnimalMapper.selectById(anId);
        animal.setAdopt(adopt);
        int n=anAnimalMapper.updateById(animal);
        if(n>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "捐款")
    @PostMapping("/donation")
    public void updOrderByidaaa(HttpServletRequest request, HttpServletResponse response,@RequestParam("anId") int anId,@RequestParam("price") float price) throws  Exception{
        AnAnimal animal=anAnimalMapper.selectById(anId);
       float now=animal.getPrice();
        float totle=now+price;
        animal.setPrice(totle);
        int n=anAnimalMapper.updateById(animal);
        if(n>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

